package com.springboot_gradrecruit.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot_gradrecruit.common.PageRequest;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.common.R;
import com.springboot_gradrecruit.model.dto.video.AddVideoRequest;
import com.springboot_gradrecruit.model.dto.video.PageVideoRequest;
import com.springboot_gradrecruit.model.dto.video.UpdateVideoRequest;
import com.springboot_gradrecruit.model.entity.Job;
import com.springboot_gradrecruit.model.entity.Video;
import com.springboot_gradrecruit.model.vo.job.JobVO;
import com.springboot_gradrecruit.model.vo.video.VideoVO;
import com.springboot_gradrecruit.service.VideoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zr
 * 宣讲会相关接口
 */
@Tag(name = "宣讲会相关接口")
@RestController
@RequestMapping("/video")
@Slf4j
public class VideoController {

    @Resource
    private VideoService videoService;

    //1.添加宣讲会
    @Operation(summary = "添加宣讲会")
    @PostMapping("/addVideo")
    public R<Boolean> addVideo(@RequestBody AddVideoRequest addVideoRequest, HttpServletRequest request){

        //1. 参数校验
        if (addVideoRequest == null || request == null) {
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成添加
        Long aLong = videoService.addVideo(addVideoRequest, request);
        if (aLong > 0) {
            return R.success(true, "添加成功");
        }
        return R.error(false, "添加失败");
    }

    //2.查询宣讲会根据绑定的企业id
    @Operation(summary = "查询宣讲会根据绑定的企业id")
    @PostMapping("/queryVideoByEnterpriseUserId")
    public R<VideoVO> queryVideoByEnterpriseUserId(Long enterpriseUserId){

        //1. 参数校验
        if (enterpriseUserId == null) {
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        VideoVO videoVO = videoService.queryVideoByEnterpriseUserId(enterpriseUserId);
        return R.success(videoVO, "查询成功");
    }

    //3.分页获取宣讲会
    @Operation(summary = "分页获取宣讲会")
    @PostMapping("/pageVideo")
    public R<PageResponse<VideoVO>> pageVideo(@RequestBody PageVideoRequest pageVideoRequest, HttpServletRequest request){
        //1. 参数校验
        if (pageVideoRequest == null || request == null) {
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        PageResponse<VideoVO> pageVideo = videoService.pageVideo(pageVideoRequest);
        return R.success(pageVideo, "查询成功");


    }

    //4.获取宣讲会详情
    @Operation(summary = "获取宣讲会详情")
    @PostMapping("/queryVideoDetailByVideoId")
    public R<VideoVO> queryVideoDetailById(Long videoId){

        //1. 参数校验
        if (videoId == null) {
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        Video video = videoService.getById(videoId);
        VideoVO videoVO = BeanUtil.copyProperties(video, VideoVO.class);
        return R.success(videoVO, "查询成功");
    }

    //5.更新宣讲会信息
    @Operation(summary = "更新宣讲会信息")
    @PostMapping("/updateVideoById")
    public R<Boolean> updateVideoById(@RequestBody UpdateVideoRequest updateVideoRequest, HttpServletRequest request){

        //1. 参数校验
        if (updateVideoRequest == null || request == null) {
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成更新
        boolean updateVideo = videoService.updateById(updateVideoRequest);
        return R.success(updateVideo, "更新成功");
    }

    @Operation(summary = "管理员-更改宣讲会审核状态")
    @PostMapping("/updateVideoStatus")
    public R<Boolean> updateVideoStatus(Long videoId,String videoCertificationStatus) {
        //1. 参数校验
        if(videoId==null||videoCertificationStatus==null){
            return R.error(null, "参数为空");
        }
        //2. 调用service完成查询
        try {
            // 先从宣讲会服务中根据宣讲会ID获取宣讲会相关信息
            Video video = videoService.getById(videoId);
            if (video == null) {
                return R.error(null, "找不到对应的宣讲会记录");
            }
            // 3. 修改宣讲会-管理状态
            video.setVideoCertificationStatus(videoCertificationStatus);
            boolean updateVideoResult = videoService.updateById(video);
            if (!updateVideoResult) {
                return R.error(null, "更新宣讲会状态失败");
            }
            return R.success(null, "更新成功");
        } catch (Exception e) {
            log.error("更新用户和企业状态出现异常", e);
            return R.error(null, "更新出现异常");
        }
    }

    @Operation(summary = "管理员-更改宣讲会管理状态")
    @PostMapping("/updateVideoAdminStatus")
    public R<Boolean> updateVideoAdminStatus(Long videoId,Integer videoAdminStatus) {
        //1. 参数校验
        if(videoId==null||videoAdminStatus==null){
            return R.error(null, "参数为空");
        }
        //2. 调用service完成查询
        try {
            // 先从宣讲会服务中根据宣讲会ID获取宣讲会相关信息
            Video video = videoService.getById(videoId);
            if (video == null) {
                return R.error(null, "找不到对应的宣讲会记录");
            }
            // 3. 修改宣讲会-管理状态
            video.setVideoAdminStatus(videoAdminStatus);
            boolean updateVideoResult = videoService.updateById(video);
            if (!updateVideoResult) {
                return R.error(null, "更新宣讲会状态失败");
            }
            return R.success(null, "更新成功");
        } catch (Exception e) {
            log.error("更新用户和企业状态出现异常", e);
            return R.error(null, "更新出现异常");
        }
    }


    //6.删除宣讲会
    @Operation(summary = "删除宣讲会")
    @PostMapping("/deleteVideo")
    public R<Boolean> deleteVideo(Long videoId){

        //1. 参数校验
        if (videoId == null) {
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成删除
        boolean b = videoService.removeById(videoId);
        return b?R.success(true,"删除成功"):R.error(false,"删除失败");
    }



}
